package model;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
 * IOProcess consists with data of the process 
 * and the table of its steps and Actions. 
 * 
 * @author DoubleS
 * @version 1.0
 */
public class IOProcess extends Process
{  
  /**
   * Construct an IO process.
   * @param the_name The name.
   */
  public IOProcess(final String the_name)
  {
    super(the_name);    
    setActionMap(generateActionMap(getNumAction()));
    //super.setMaxStep();
  }
  
  /**
   * 
   * @param the_num_entries the number of steps of this process
   * @return The map of steps and actions corresponding the given
   *         the_num_entries
   */
  private Map<Integer, ProcessAction> generateActionMap(final int the_num_entries)
  {
    final Map<Integer, ProcessAction> the_action_map =
        new HashMap<Integer, ProcessAction>(the_num_entries);
    final Random random = new Random();
    final ProcessAction the_action;
    if (random.nextBoolean())
    {
      the_action = ProcessAction.IO_REQUEST_1;
    }
    else
    {
      the_action = ProcessAction.IO_REQUEST_2;
    }
    int the_temp_num;
    
    /*
     * Loop through the_num_entries and add elements to the table after checking
     * steps aren't repeated
     */
    for (int i = 0; i < the_num_entries; i++)
    {
      do
      {
        the_temp_num = random.nextInt(getMaxStep());
      }
      while (the_action_map.containsKey(the_temp_num));
      
      the_action_map.put(the_temp_num, the_action);
    }
    return the_action_map;
  }

}

